package com.zhudi.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhudi.annotation.AuthCheck;
import com.zhudi.annotation.UserLoginCheck;
import com.zhudi.commen.BaseResponse;
import com.zhudi.expection.BusinessException;
import com.zhudi.model.entity.Interface;
import com.zhudi.model.entity.User;
import com.zhudi.model.request.*;
import com.zhudi.model.vo.InterfaceVO;
import com.zhudi.service.InterfaceService;
import com.zhudi.utils.ResultUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import java.util.Map;

import static com.zhudi.commen.ErrorCode.PARAMS_ERROR;
import static com.zhudi.contant.PageContant.DEFAULT_PAGE_NUM;
import static com.zhudi.contant.PageContant.DEFAULT_PAGE_SIZE;
import static com.zhudi.contant.UserContant.USER_LOGIN_STATE;

@RestController
@RequestMapping("/interfaceInfo")
public class InterfaceController {

    @Resource
    InterfaceService interfaceService;

    @PostMapping("/list/page")
    BaseResponse<Page<InterfaceVO>> listInterfaceInfoByPage(@RequestBody PageRequest pageRequest){
        //检查参数是否为空
        if(pageRequest==null){
            throw new BusinessException(PARAMS_ERROR);
        }
        Integer pageNum = pageRequest.getPageNum();
        Integer pageSize = pageRequest.getPageSize();
        //如果参数不合法则默认显示第一页前十条
        if(pageNum==null){
            pageRequest.setPageNum(DEFAULT_PAGE_NUM);

        }
        if(pageSize==null){
            pageRequest.setPageSize(DEFAULT_PAGE_SIZE);

        }
        //不为空的话查询数据库然后封装返回
        Page<InterfaceVO> result = interfaceService.listInterfaceByPage(pageRequest);

        return ResultUtils.success(result,"查询成功");

    }

    @GetMapping("/get")
    BaseResponse<InterfaceVO> getInterfaceInfoByid(Long id){
        //检查参数是否为空
        if(id==0){
            throw new BusinessException(PARAMS_ERROR);
        }

        Interface result = interfaceService.getById(id);
        InterfaceVO interfaceVO  = new InterfaceVO();
        if (result==null){
            throw new BusinessException(PARAMS_ERROR,"该接口不存在");
        }

        BeanUtils.copyProperties(result,interfaceVO);

        return ResultUtils.success(interfaceVO,"查询成功");

    }

    @PostMapping("/add")
    @AuthCheck(role = "ADMIN")
    BaseResponse<Integer> addInterfaceInfo(@RequestBody InterfaceAddRequest interfaceAddRequest, HttpServletRequest request){
        if(interfaceAddRequest==null){
            throw new BusinessException(PARAMS_ERROR);
        }
        User loginUser = (User) request.getSession().getAttribute(USER_LOGIN_STATE);
        //todo: 要给这个方法加锁，防止一下子超快点击创建了多个接口
        //todo 这里的参数暂时设置为不是必填的。后续再看看需不需要改
        int interfacID = interfaceService.addInterface(interfaceAddRequest, loginUser);

        return ResultUtils.success(interfacID,"执行成功");

    }
    @PostMapping("/delete")
    @UserLoginCheck
    BaseResponse<Boolean> deleteInterfaceInfo(@RequestBody IdRequest idRequest, HttpServletRequest request){
        if (idRequest.getId()==null||idRequest.getId()<=0){
            throw new BusinessException(PARAMS_ERROR);
        }

        User loginUser = (User) request.getSession().getAttribute(USER_LOGIN_STATE);


        Boolean result = interfaceService.deleteInterface(idRequest.getId(), loginUser);

        return ResultUtils.result(result);
    }
    @PostMapping("/update")
    BaseResponse<Boolean> updateInterfaceInfo(@RequestBody InterfaceUpdateRequest interfaceUpdateRequest, HttpServletRequest request){
        User loginUser = (User) request.getSession().getAttribute(USER_LOGIN_STATE);


        int result = interfaceService.updateInterface(interfaceUpdateRequest, loginUser);

        return ResultUtils.result(result==1);
    }
    @PostMapping("/online")
    @AuthCheck(role = "ADMIN")
    BaseResponse<Boolean> onlineInterfaceInfo(@RequestBody IdRequest idRequest){
        return ResultUtils.result(interfaceService.onlineInterface(idRequest.getId())==1);
    }
    @PostMapping("/offline")
    BaseResponse<Boolean> offlineInterfaceInfo(@RequestBody IdRequest idRequest){
        return ResultUtils.result(interfaceService.offlineInterface(idRequest.getId())==1);
    }

    @PostMapping("/invoke")
    BaseResponse<Map<String,Object>> invokeInterfaceInfo(@RequestBody InterfaceInvokeRequest interfaceInvokeRequest){
        return null;
    }
}
